package com.xjgc.msibase.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjgc.exo.RestServerStandardManager;
import com.xjgc.exo.WsClientStandardManager;
import com.xjgc.msi.exo.wsclient.WsClientStandardRequest;
import com.xjgc.msi.exo.wsclient.WsClientStandardResponse;
import com.xjgc.msibase.constant.Constant;
import com.xjgc.msibase.entity.MsiExoServer;
import com.xjgc.msibase.service.IMsiExoServerService;
import com.xjgc.msibase.service.IMsiExoTaskServerService;
import com.xjgc.msibase.vo.MsiExoSystemServerVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoDict;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.util.UUIDGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * 外围服务接口
 *
 * @Description: 外围服务接口
 * @Author: 小逗号
 * @Date: 2023-10-30
 * @Version: V1.0
 */
@Api(tags = "外围服务接口")
@RestController
@RequestMapping("/msibase/msiExoServer")
@Slf4j
public class MsiExoServerController extends JeecgController<MsiExoServer, IMsiExoServerService> {
    @Resource
    private IMsiExoServerService msiExoServerService;
    @Resource
    private IMsiExoTaskServerService msiExoTaskServerService;


    /**
     * 分页列表查询
     *
     * @param queryAo
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "外围服务接口-分页列表查询", notes = "外围服务接口-分页列表查询")
    @AutoDict
    @GetMapping(value = "/list")
    public Result<IPage<MsiExoSystemServerVO>> queryPageList(MsiExoSystemServerVO queryAo,
                                                             @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                             @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        IPage<MsiExoSystemServerVO> page = msiExoServerService.querySystemServerPage(new Page<MsiExoSystemServerVO>(pageNo, pageSize), queryAo);
        return Result.OK(page);
    }

    @ApiOperation(value = "外围服务接口-新增", notes = "外围服务接口-新增")
    @PostMapping("/add")
    public Result<?> add(@RequestBody MsiExoSystemServerVO addAo) {
        Result<?> result = msiExoServerService.addServer(addAo);
        return result;
    }

    @ApiOperation(value = "外围服务接口-编辑修改", notes = "外围服务接口-编辑修改")
    @PutMapping("/edit")
    public Result<?> edit(@RequestBody MsiExoSystemServerVO editAo) {
        MsiExoServer msiExoServer = new MsiExoServer();
        BeanUtils.copyProperties(editAo, msiExoServer);
        msiExoServerService.updateById(msiExoServer);
        return Result.OK();
    }

    @Resource
    RestTemplate restTemplate;
    @ApiOperation("外围服务接口-测试")
    @PostMapping("/testExoServer")
    public Result<?> testExoServer(@RequestBody MsiExoServer testDto) {
        String exoServerType = testDto.getExoServerType();
        if (Constant.SERVER_TYPE_WS.equals(exoServerType)) {
            WsClientStandardManager wsClientManager = new WsClientStandardManager();
            WsClientStandardRequest request = new WsClientStandardRequest();
            request.setTaskName(testDto.getTaskName());
            request.setSerialNo(UUIDGenerator.generate());
            request.setData(testDto.getRequestText());
            request.setExtend("中间库外围服务接口注册测试");
            WsClientStandardResponse response = wsClientManager.callWs(testDto, request);
            return Result.OK(response);
        } else if (Constant.SERVER_TYPE_REST.equals(exoServerType)) {
            String requestText = testDto.getRequestText();
            RestServerStandardManager restServerStandardManager = new RestServerStandardManager();
            
            //restServerStandardManager.callRest(testDto,);


            return Result.error("REST接口服务正在开发中!");
        } else {
            return Result.error("未知的服务接口类型! " + exoServerType);
        }
    }

    @ApiOperation("根据任务id查询关联的接口")
    @GetMapping(value = "/listByTaskId")
    public Result<?> listByTaskId(String taskId) {
        List<MsiExoServer> list = msiExoTaskServerService.queryByTaskId(taskId);
        return Result.ok(list);
    }
}
